package command;

import java.util.HashSet;
import java.util.Iterator;

import model.impl.ScheduleImpl;
import DTO.ScheduleDTO;

import com.gmail.at.vssabinin.train_transport_system_part1.Protocol;
import com.gmail.at.vssabinin.train_transport_system_part1.ReceivedData;

/** 
 * Class represents command interface;
 * It checks that the received schedules from client are unique,
 * and if they are create and save them.
 */
public class AdminCreateNewSchedule implements Command {
	
	private Protocol protocol = new Protocol();

	@Override
	public void execute(ReceivedData recived) {
		Protocol.Message message = this.protocol.decodeMessage(recived.data);
		@SuppressWarnings("unchecked")
		HashSet<ScheduleDTO> schedules = (HashSet<ScheduleDTO>) message.getObject();
		Iterator<ScheduleDTO> iter = schedules.iterator();
		while(iter.hasNext()) {
			ScheduleDTO schedule = iter.next();
			(new ScheduleImpl()).saveUniqueSchedule(schedule);
		}
		Protocol.Message answer = this.protocol.new Message("ok", null);
		recived.server.send(recived.socket, this.protocol.createMessage(answer));
	}

}

